<template>
  <el-upload
    class="pdf-uploader"
    :name="uploadNameUse"
    :action="UploadUrl"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :on-exceed="handleExceed"
    :on-success="handleUploadSuccess"
    :limit="1"
    :file-list="fileList"
    accept=".pdf"
  >
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传pdf文件，且不超过100M</div>
  </el-upload>
</template>

<script>
export default {
  name: 'PdfUpload',
  props: {
    uploadName: {
      required: true,
      type: String
    },
    uploadUrl: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      UploadUrl: process.env.VUE_APP_BASE_API + '/site/file-upload',
      uploadNameUse: this.uploadName,
      uploadUrlUse: this.uploadUrl,
      fileList: [
      ]
    }
  },
  watch: {
    uploadName: {
      handler(newVal, oldVal) {
        this.uploadNameUse = newVal
      }
    },
    uploadImageUrl: {
      handler(newVal, oldVal) {
        this.uploadUrlUse = newVal
      }
    }
  },
  methods: {
    handleRemove(file, fileList) {
    },
    handlePreview(file) {
    },
    handleUploadSuccess(res, file) {
      this.uploadUrlUse = URL.createObjectURL(file.raw)
      if (res.code !== 200) {
        this.$message.warning(res.message)
      } else {
        this.$emit('getReturnUrl', res.data)
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    }
  }
}
</script>
